package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.StorageBean;
import com.project.mapper.IStorageMapper;
import com.project.service.IStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class StorageServiceImpl implements IStorageService {
    @Autowired
    private IStorageMapper storageMapper;

    @Override
    public IPage<StorageBean> cutByItem(Integer pageNO, String name, Integer minNum,
                                        Integer maxNum, String price, String type) {
        QueryWrapper<StorageBean> qw = new QueryWrapper<>();
        if (name != null && name.length() != 0) {
            qw.like("p_name", name);
        }
        if (minNum != null) {
            qw.ge("p_num", minNum);
        }
        if (maxNum != null) {
            qw.le("p_num", maxNum);
        }
        if ("未定售价".equals(price)) {
            qw.eq("p_oprice", 0);
        }else if ("已定售价".equals(price)){
            qw.ge("p_oprice",1);
        }
        if (type != null && type.length() != 0) {
            qw.eq("p_type", type);
        }
        return storageMapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public StorageBean findById(Integer id) {
        return storageMapper.selectById(id);
    }

    @Override
    public void update(Integer id, Integer price) {
        StorageBean storageBean = storageMapper.selectById(id);
        storageBean.setoPrice(price);
        storageMapper.updateById(storageBean);
    }

}
